mui.init({
	gestureConfig: {
		longtap: true
	}
});
//初始化单页view
var viewApi = mui('#app').view({
	defaultPage: '#setting'
});
//初始化单页的区域滚动
mui('.mui-scroll-wrapper').scroll();

mui.plusReady(function() {
	initNotes(); //初试化缓存
	parper();
});
(function($) {
	//处理view的后退与webview后退
	var oldBack = $.back;
	$.back = function() {
		if(viewApi.canBack()) { //如果view可以后退，则执行view的后退
			viewApi.back();
		} else { //执行webview后退
			oldBack();
		}
	};
})(mui);

$(function() {
	//打开新增页面
	document.getElementById("open").addEventListener('tap', function() {
		$("#name").val("");
		$("#type").val("");
		$("#content").val("");
		$("#title").html("新增");
		$("#add_div").removeClass("mui-hidden");
		$("#edit_div").addClass("mui-hidden");
		mui("#popover").popover("toggle");
	});
	
	//新增操作
	document.getElementById("add").addEventListener('tap', function() {
		var check = true;
		mui(".mui-input-group input").each(function() {
			if(!this.value || trim(this.value) == "") {
				var label = this.previousElementSibling;
				mui.alert(label.innerText + "不允许为空");
				check = false;
				return false;
			}
		});
		if(check) {
			var ref = sun.createLink("/note/" + $("#type").val());
			var newRef = ref.push({
				"name": $("#name").val(),
				"type": $("#type").val(),
				"content": $("#content").val(),
				"userId": app.getUser().account
			});
			$("#add_div").addClass("mui-hidden");
			//关闭modal
			mui("#popover").popover("toggle");
			mui.toast("添加成功~");
		}
	});
	
	//更新
	document.getElementById("update").addEventListener("tap", function() {
		var check = true;
		mui(".mui-input-group input").each(function() {
			if(!this.value || trim(this.value) == "") {
				var label = this.previousElementSibling;
				mui.alert(label.innerText + "不允许为空");
				check = false;
				return false;
			}
		});
		if(check) {
			initParams()
			var ref = sun.createLink("/note");
			var newRef = ref.child($("#name").attr("data-id"));
			newRef.update({
				"name": $("#name").val(),
				"content": $("#content").val(),
				"type": $("#type").val()
			});
			//关闭modal
			mui("#popover").popover("toggle");
			$("#edit_div").addClass("mui-hidden")
			mui.toast("修改成功~");
			//初试化缓存
			//加载未完成列表
			//初始化已完成列表
		}
	});
	//删除操作
	document.getElementById("del").addEventListener("tap", function() {
		var ref = sun.createLink("/note");
		var newRef = ref.child($("#name").attr("data-id"));
		newRef.remove();
		//关闭modal
		mui("#popover").popover("toggle");
		$("#edit_div").addClass("mui-hidden");
		mui.toast("删除成功~");
		//刷新界面
		//初试化缓存
		//加载未完成列表
		//初始化已完成列表
	});
});
function editOption(e) {
	//进入编辑列表
	//mui(".notess").on("tap", "li", function() {
		var el = $(e);
		var id = el.attr("data-id");
		if(id) {
			$("#name").val(el.find("#name_").val());
			$("#content").val(el.find("#content_").val());
			$("#type").val(el.find("#type_").val());
			$("#name").attr("data-id", id);
			$("#title").html("编辑数据");
			$("#add_div").addClass("mui-hidden");
			$("#edit_div").removeClass("mui-hidden");
			mui("#popover").popover("toggle");
		}
	//});
}
function trim(str) { //删除左右两端的空格
	return str.replace(/(^\s*)|(\s*$)/g, "");　　
}

function initNotes() {
	var ref = sun.createLink("/note");
	var notes = {};
	ref.on("value", function(snapshot) {
		var result = snapshot.val();
		mui.each(result, function(key, item) {
			var typeArray = [];
			mui.each(item, function(a, b) {
				var onlyData = {};
				onlyData = sun.cloneObj(b);
				onlyData.id = a;
				typeArray.push(onlyData);
			});
			notes[key] = typeArray;
		});
		localStorage.setItem("$notes", JSON.stringify(notes));
	});
}

function parper() {
	var notes = JSON.parse(localStorage.getItem('$notes'));
	var lis = "";
	for(var o in notes) {
		var li = "";
		var type = o;
		var id = "card-" + parseInt(Math.random() * 100000);
		li += "<li class='mui-table-view-cell'>";
		li += "<a href='#" + id + "' class='mui-navigate-right'>" + type + "</a>";
		li += "</li>";
		lis += li;

		var data = notes[type];
		var page = "";
		page += '<div id="' + id + '" class="mui-page">';
		page += '<div class="mui-navbar-inner mui-bar mui-bar-nav">';
		page += '<button type="button" class="mui-left mui-action-back mui-btn  mui-btn-link mui-btn-nav mui-pull-left">';
		page += '<span class="mui-icon mui-icon-left-nav"></span>';
		page += '分类</button>';
		page += '<h1 class="mui-center mui-title">' + type + '</h1>';
		page += '</div>';
		page += '<div class="mui-page-content">';
		page += '<div class="mui-scroll-wrapper">';
		page += '<div class="mui-scroll">';
		page += '<ul class="mui-table-view notess">';
		for(var d in data) {
			page += '<li data-id="' + data[d].id + '" onclick="editOption(this)">';
			page += '<div class="mui-card">';
			page += '<div class="mui-card-header">';
			page += '<div class="mui-media-body">';
			page += data[d].name;
			page += '<p>发表于 2016-06-30 15:30</p>';
			page += '</div>';
			page += '</div>';
			page += '<div class="mui-card-content">';
			page += '<pre>'
			page += data[d].content;
			page += '</pre>'
			page += '</div>';
//			page += '<div class="mui-card-footer"></div>';
//			page += '</div>';
			page += '<input type=hidden id="name_" value="' + data[d].name + '"/>';
			page += '<input type=hidden id="type_" value="' + data[d].type + '"/>';
			page += '<input type=hidden id="content_" value="' + data[d].content + '"/>';
			page += '</li>';
		}
		page += '</ul>';
		page += '</div>';
		page += '</div>';
		page += '</div>';
		$(".submain").after(page);
	}
	$(".ulList").html(lis);
}